package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysMeetingMapper;
import com.ruoyi.system.domain.SysMeeting;
import com.ruoyi.system.service.ISysMeetingService;

/**
 * 会议Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-23
 */
@Service
@Slf4j
public class SysMeetingServiceImpl implements ISysMeetingService {
    @Autowired
    private SysMeetingMapper sysMeetingMapper;

    /**
     * 查询会议
     *
     * @param roomId 会议主键
     * @return 会议
     */
    @Override
    public SysMeeting selectSysMeetingByRoomId(Long roomId) {
        return sysMeetingMapper.selectSysMeetingByRoomId(roomId);
    }

    /**
     * 查询会议列表
     *
     * @param sysMeeting 会议
     * @return 会议
     */
    @Override
    public List<SysMeeting> selectSysMeetingList(SysMeeting sysMeeting) {
        return sysMeetingMapper.selectSysMeetingList(sysMeeting);
    }

    /**
     * 新增会议
     *
     * @param sysMeeting 会议
     * @return 结果
     */
    @Override
    public int insertSysMeeting(SysMeeting sysMeeting) {
        sysMeeting.setCreateTime(DateUtils.getNowDate());
        return sysMeetingMapper.insertSysMeeting(sysMeeting);
    }

    /**
     * 修改会议
     *
     * @param sysMeeting 会议
     * @return 结果
     */
    @Override
    public int updateSysMeeting(SysMeeting sysMeeting) {
        return sysMeetingMapper.updateSysMeeting(sysMeeting);
    }

    /**
     * 批量删除会议
     *
     * @param roomIds 需要删除的会议主键
     * @return 结果
     */
    @Override
    public int deleteSysMeetingByRoomIds(Long[] roomIds) {
        return sysMeetingMapper.deleteSysMeetingByRoomIds(roomIds);
    }

    /**
     * 删除会议信息
     *
     * @param roomId 会议主键
     * @return 结果
     */
    @Override
    public int deleteSysMeetingByRoomId(Long roomId) {
        return sysMeetingMapper.deleteSysMeetingByRoomId(roomId);
    }

    @Scheduled(cron = "0 */10 * * * ?")
    public void endMeetingTask() {
        log.info("endMeetingTask start.......");
        SysMeeting meeting = new SysMeeting();
        meeting.setStatus("0");
        List<SysMeeting> list = selectSysMeetingList(meeting);
        Date now = new Date();
        for (SysMeeting m : list) {
            if (m.getRespectEndTime() != null && now.compareTo(m.getRespectEndTime()) > 0) {
                SysMeeting vo = new SysMeeting();
                vo.setRoomId(m.getRoomId());
                vo.setEndTime(m.getRespectEndTime());
                vo.setStatus("1");
                updateSysMeeting(vo);
                log.info(m.getContent() + "----结束状态改变----" + now);
            }
        }
        log.info("endMeetingTask end.......");

    }

}
